package com.ittsang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittsang.dto.PageDTO;
import com.ittsang.dto.R;
import com.ittsang.dto.UserApplicationDTO;
import com.ittsang.dto.UserDTO;
import com.ittsang.mapper.DepartmentMapper;
import com.ittsang.mapper.UserApplicationMapper;
import com.ittsang.mapper.UserClubPostsMapper;
import com.ittsang.mapper.UserMapper;
import com.ittsang.pojo.Article;
import com.ittsang.pojo.User;
import com.ittsang.pojo.UserApplication;
import com.ittsang.pojo.UserClubPosts;
import com.ittsang.service.UserApplicationService;
import com.ittsang.utils.UserHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserApplicationServiceImpl implements UserApplicationService {

    @Resource
    private UserApplicationMapper userApplicationMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private UserClubPostsMapper userClubPostsMapper;

    @Override
    public R insertClubUser(UserApplicationDTO userApplicationDTO) {
        UserDTO user = UserHolder.getUser();
        if (!user.getClubId().equals(0)) {
            return R.error("加入失败，当前用户已有社团");
        }
        boolean i = userApplicationMapper.insertClubUser(userApplicationDTO);
        return i ? R.ok("申请加入成功，等待社团审核") : R.error();
    }

    @Override
    public R getClubAuditUser(PageDTO pageDTO) {
        UserDTO user = UserHolder.getUser();
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), user.getClubId());
        if (user.getClubId() == 0) {
            R.error("用户无权限");
        }
        if (userClubPosts == null) {
            return R.error("用户无权限");
        }
        List<UserApplication> userApplications = userApplicationMapper.getClubAuditUser(user.getClubId());
        for (UserApplication userApplication : userApplications) {
            userApplication.setUserName(userMapper.getUserName(userApplication.getUserId()));
            userApplication.setDepartmentName(departmentMapper.getDepartment(user.getClubId(), userApplication.getDepartmentId()).getName());
        }

        //分页操作
        int pageNum = pageDTO.getPageNum();
        int pageSize = pageDTO.getPagesize();
        PageHelper.startPage(pageNum,pageSize);
        int skip= (pageNum-1)*pageSize;
        Page<UserApplication> page = new Page<>(pageNum, pageSize);
        page.setTotal(userApplications.size());
        List<UserApplication> articleDTOList = userApplications.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(articleDTOList);

        return R.ok("获取成功").put("userApplications", page).put("total", page.getTotal());
    }

    @Override
    public R isAuditUser(Integer userApplicationId) {
        // 判断用户权限
        UserDTO user = UserHolder.getUser();
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user.getId()), user.getClubId());
        if (userClubPosts == null) {
            return R.error("用户无权限");
        }
        boolean i = userApplicationMapper.isAuditUser(userApplicationId);
        return i ? R.ok("取消成功") : R.error("取消失败");
    }

    @Override
    @Transactional
    public R isOkAuditUser(Integer userApplicationId) {
        // 判断用户权限
        UserDTO user1 = UserHolder.getUser();
        UserClubPosts userClubPosts = userClubPostsMapper.getUserRole(Math.toIntExact(user1.getId()), user1.getClubId());
        if (userClubPosts == null) {
            return R.error("用户无权限");
        }
        // 判断申请用户是否已经加入社团
        UserApplication userApplication = userApplicationMapper.getUserApplication(userApplicationId);
        User user = userMapper.getUserById(userApplication.getUserId());
        if (user.getClubId() != 0) {
            return R.error("该用户已有社团，不能通过申请");
        }
        boolean i = userMapper.alterUserClub(userApplication.getUserId(), userApplication.getDepartmentId(), userApplication.getClubId());
        if (i) {
            userApplicationMapper.isAuditUser(userApplicationId);
        }

        return i ? R.ok("通过成功") : R.error("通过失败");
    }
}
